﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
using SSPharma.Biz;
using SSPharma.Model;
using SSPharma.Command;
using System.Windows.Input;

namespace SSPharma.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        #region Fields

        private ObservableCollection<MenuItemViewModel> _menuItem;
        private RelayCommand cmdAdd;
        private RelayCommand cmdRefresh;
        private string _currentMenuName;
        private object _currentMenuViewModel;
        private RelayCommand cmdHienThiForm;
        //private Form m_frm_CurrentForm = null
//        public ObservableCollection<Person> Persons { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand RefreshCommand
        {
            get
            {
                if (cmdRefresh == null)
                {
                    cmdRefresh = new RelayCommand(
                        param => this.Refresh(param));
                }
                return cmdRefresh;
            }
        }
        public ICommand AddCommand
        {
            get
            {
                if (cmdAdd == null)
                {
                    cmdAdd = new RelayCommand(
                        param => this.Add(param));
                }
                return cmdAdd;
            }
        }

        public void Refresh(object sender)
        {
            Load_DanhMuc();
        }
        public void Add(object sender)
        {
            string a = "";
        }
        #endregion

        #region Constructor
        public MainWindowViewModel()
        {
            _menuItem = new ObservableCollection<MenuItemViewModel>();
            Load_DanhMuc();
            _currentMenuViewModel = new SSPharma.View.HeThong.Desktop();
        }
        #endregion

        #region Properties

        public ObservableCollection<MenuItemViewModel> MenuItems
        {
            get
            {
                return _menuItem;
            }
            set
            {
                _menuItem = value;
                base.OnPropertyChanged("MenuItems");
            }
        }

        public string CurrentMenuName
        {
            get
            {
                return _currentMenuName;
            }
            set
            {
                _currentMenuName = value;
                base.OnPropertyChanged("CurrentMenuName");
            }
        }
    
        public object CurrentMenuViewModel
        {
            get
            {
                return _currentMenuViewModel;
            }
            set
            {
                try
                {
                    _currentMenuViewModel = value;
                    base.OnPropertyChanged("CurrentMenuViewModel");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
 
        #endregion

        #region Private Methods

        private void Load_DanhMuc()
        {
            /* Lay danh sach danh muc va menu*/
            List<MenuItem> lsDanhMuc_Menu = BizMainWindow.DSDanhMuc_Menu();

            MenuItem rootMenuItem = new SSPharma.Model.MenuItem();
            rootMenuItem.MenuName = "Danh Mục";
            rootMenuItem.MenuID = BizMainWindow.ROOT_MENU;

            MenuItemViewModel rootMenuItemModel = new MenuItemViewModel(rootMenuItem, this);


            for (int i = 0; i < lsDanhMuc_Menu.Count; i++)
            {
                MenuItemViewModel menuItemModel = rootMenuItemModel.GetMenuItemModelByID(lsDanhMuc_Menu[i].ParentID);

                if (menuItemModel != null)
                {
                    menuItemModel.ChildMenuCollection.Add(new MenuItemViewModel(lsDanhMuc_Menu[i], this));
                }
            }

            MenuItems = rootMenuItemModel.ChildMenuCollection;
        }
        

        #endregion

    }

}
